package com.shouhuyun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.entity.PageResult;
import com.shouhuyun.entity.QueryPageBean;
import com.shouhuyun.entity.Result;
import com.shouhuyun.pojo.Role;
import com.shouhuyun.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    // 引入service
    @Reference
    private RoleService roleService;


    //分页查询所有角色信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;

    }

    // 增添角色信息
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menusIds) {
        try {
            roleService.add(role, permissionIds, menusIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

    }

    //    根据角色的id  查询角色信息用于基本信息回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            // 查询角色成功
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            // 查询角色失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    // 根据角色ID查询角色包含的多个权限id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            // 查询角色相关权限成功
            return new Result(true, MessageConstant.QUERY_PERMISSIONBYROLEID_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            // 查询角色相关权限失败
            return new Result(false, MessageConstant.QUERY_PERMISSIONBYROLEID_FAIL);
        }
    }


    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_ROLE_FAIL);
        }
    }


    // 根据角色ID查询角色包含的多个菜单id
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> menusIds = roleService.findMenuIdsByRoleId(id);
            // 查询角色相关权限成功
            return new Result(true, MessageConstant.QUERY_MENUBYROLEID_SUCCESS, menusIds);
        } catch (Exception e) {
            e.printStackTrace();
            // 查询角色相关权限失败
            return new Result(false, MessageConstant.QUERY_MENUBYROLEID_FAIL);
        }

    }

    // 编辑 角色信息(权限菜单的更新)
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menusIds) {
        try {
            roleService.edit(role, permissionIds, menusIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }

    }

    //    根据角色的id  查询角色信息用于基本信息回显
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
            // 增添角色成功
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 删除角色失败
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}

